package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.LoginServiceImpl;
import com.javasm.util.MD5Util;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/14 18:07
 * @since JDK 1.8
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
//        System.out.println(method);
        Map<String, Object> returnData = new HashMap<>();
        AdminUser adminUser = null;
        PrintWriter pw = resp.getWriter();
        if ("isExist".equals(method)) {
            adminUser = isExist(req, resp);
        } else if ("submit".equals(method)) {
            adminUser = login(req, resp);
        } else if ("updateLoginTime".equals(method)) {
            updateLoginTime(req, resp);
            resp.sendRedirect(req.getContextPath() + "/main.html");
            return;
        } else if ("logout".equals(method)) {
            req.getSession().removeAttribute("loginAccount");
            resp.sendRedirect(req.getContextPath() + "/login.html");
            return;
        } else if ("chargePwd".equals(method)) {  // 修改密码时做的密码校验
            adminUser = chargePwd(req, resp);
        }
        // 判断查询条件
        if (adminUser == null) {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnMsg", ReturnCode.ERROR.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnData", adminUser);
            if ("submit".equals(method)) { // 如果是登录检测的方式能查到值，说明登陆成功，账号信息存入session
                req.getSession().setAttribute("loginAccount", adminUser);
            }
        }
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 修改密码时校验旧密码的回调
    private AdminUser chargePwd(HttpServletRequest req, HttpServletResponse resp) {
        String pwd = MD5Util.md5(req.getParameter("pwd"));
        System.out.println(pwd);
        AdminUser exist = (AdminUser)req.getSession().getAttribute("loginAccount");
        if (pwd.equals(exist.getApwd())) {
            return exist;
        } else {
            return null;
        }
    }

    // 更新登陆时间
    private void updateLoginTime(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        new LoginServiceImpl().updateLoginTime(name);
    }

    // 登录校验
    private AdminUser login(HttpServletRequest req, HttpServletResponse resp) {
        String name = req.getParameter("name");
        String pass = MD5Util.md5(req.getParameter("pass"));
        System.out.println(pass);
        AdminUser exist = new LoginServiceImpl().isExist(name);
        if (pass.equals(exist.getApwd())) {
            return exist;
        } else {
            return null;
        }
    }

    // 判断用户名是否存在
    private AdminUser isExist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        return new LoginServiceImpl().isExist(name);
    }
}
